/*
 * Menu: Actionscript > Generate Get/Set Properties With Binding
 * Key: M3+8
 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
 */
 
function main() 
{
  var editor = editors.activeEditor
  var source = editor.source
  
  if (editor.selectionRange) {
    var range  = editor.selectionRange
  var offset = range.startingOffset
  
  var text = source.substring (offset, range.endingOffset)
  var result = text.match(/(\w+:\w+)/g);
  
  o = ""
  
  for (var i = 0, n = result.length; i < n; ++i)
    o += props(result[i])
  
  o += "\n"
  
//  debug(o)  
  editor.applyEdit(offset, range.endingOffset - offset, o)
   }  
}

function props(v) {
  var parts = v.split(":");
  
  if (2 != parts.length)
    return "";
  
  var eventName = getEventName (parts[0]);
  var privateTypeName = "_" + parts[0];
  var type = parts[1];
  var typeName = parts[0];
    
  var s = "\n\t\tprivate var " + privateTypeName + ":" + type + ";";
  s += "\n\t\t[Bindable(event=\"" + eventName + "\")]";
  s += "\n\t\tpublic function set " + typeName + " (value:" + type + "):void";
  s += "\n\t\t{";
  s += "\n\t\t\tif (value != " + privateTypeName + ")";
  s += "\n\t\t\t{";
  s += "\n\t\t\t\t" + privateTypeName + " = value;";
  s += "\n\t\t\t\tdispatchEvent (new Event (\"" + eventName + "\"));";
  s += "\n\t\t\t}";
  s += "\n\t\t}\n";
  
  
  s += "\n\t\tpublic function get " + typeName + " ():" + type;
  s += "\n\t\t{";
  s += "\n\t\t\treturn " + privateTypeName + ";" ;
  s += "\n\t\t}\n";
  
  return s;
}

function getEventName (s)
{
	result = s + "Change";
	return result;
}

//function captalize (s)
//{
//	return s;
//}

function getvar(s) {
  if (1 > s.length)
    return "";
    
  return "\n\t\tprotected var _" + s + ";";
}
 

function debug(s) {
  Packages.org.eclipse.jface.dialogs.MessageDialog.openInformation(window.getShell(), "Monkey Debugging", s);
}
